<?php
/**
 * Created by PhpStorm.
 * User: zzw
 * Date: 17-4-21
 * Time: 下午3:16
 */
class Power_Hook{
    protected $userinfo=false;
    protected $vdata=array();
    function power(){
        $CI  = &get_instance();
        $str = $CI->uri->uri_string();
        $allow = $this->_uri_allow($str);
        if(isset($_POST['token']) && $_POST['token'] && !$allow) {
            $CI->load->model('account_token_model', 'macctoken');
            $CI->load->model('account_model', 'macc');
            $CI->load->model('account_info_model', 'maccount_info');
            $token = $CI->macctoken->get_one(array('token' => $_POST['token']), 'accountId,expiretime');
            $msg = false;
            $userinfo = array();
            if ($token) {
                if ($token['expiretime'] < time()) {
                    $msg = 'token已过期，请重新登录';
                } else {
                    $this->userinfo = $CI->macc->get_one(array('id' => $token['accountId']),'id,audit,status');
                }
            } else {
                $msg = '无效token';
            }
            if ($msg) {
                $re = array(
                    'returnCode' => '401',
                    'returnInfo' => $msg,
                    'timeline' => time());
                $re = array_merge($this->vdata, $re);
                header("Content-Type: application/json; charset=utf-8");
                echo json_encode($this->_send_json_befor($re));
                exit;
            }
            if($this->userinfo) {
                $power = $this->_power();
                if($power['returnCode'] && $this->power == 1) {
                    $this->_power_msg($power['returnCode']);
                }
            }

        }
    }
    protected function _power() {
        //audit 0 未禁用 1 禁用
        //status 用户状态，0 审核不通过 1 正在审核 2 审核通过
        $uid = $this->userinfo['id'];
        if($this->userinfo['audit']) {
            $re = array(
                'returnCode' => '7000001',
                'returnInfo' => '您的帐号已被禁用',
                'timeline' => time());
            //return $re;
            $re = array_merge($this->vdata, $re);
            header("Content-Type: application/json; charset=utf-8");
            echo json_encode($this->_send_json_befor($re));
            exit;
        } else {
            $account_status = $this->userinfo['status'];
            if(!$account_status) {
                $re = array(
                    'returnCode' => '7000002',
                    'returnInfo' => '您的帐号审核未通过',
                    'timeline' => time());
                //return $re;
                $re = array_merge($this->vdata, $re);
                header("Content-Type: application/json; charset=utf-8");
                echo json_encode($this->_send_json_befor($re));
                exit;
            } elseif($account_status == 1) {
                $re = array(
                    'returnCode' => '7000003',
                    'returnInfo' => '您的账户将在24小时内审核',
                    'timeline' => time());
                $re = array_merge($this->vdata, $re);
                $this->power = 1;
                return $re;
            } else {
                $this->power = 2;
            }
            $CI  = &get_instance();
            $CI->load->model('identity_model','midentity');
            $identity = $CI->midentity->get_one(array('uid'=>$uid));
            $identity_status = $identity ? $identity['status'] : 3;
            if($identity_status != 3) {
                //身份变更 审核状态 0 提交审核 1 审核不通过 2 审核通过
                if(!$identity_status) {
                    $re = array(
                        'returnCode' => '7000004',
                        'returnInfo' => '您的帐号身份变更正在审核',
                        'timeline' => time());
                    //return $re;
                    $re = array_merge($this->vdata, $re);
                    if($account_status == 1) {
                        $this->power = 1;
                    } elseif($account_status == 2 ) {
                        $this->power = 2;
                    }
                    return $re;
                } elseif($identity_status == 1) {
                    $re = array(
                        'returnCode' => '7000005',
                        'returnInfo' => '您的帐号身份变更审核未通过',
                        'timeline' => time());
                    //return $re;
                    $re = array_merge($this->vdata, $re);
                    if($account_status == 1 || $account_status == 2) {
                        $this->power = 1;
                    }
                    return $re;
                } else {
                    if($account_status == 1) {
                        $re = array(
                            'returnCode' => '7000003',
                            'returnInfo' => '您的账户将在24小时内审核',
                            'timeline' => time());
                        $re = array_merge($this->vdata, $re);
                        $this->power = 1;
                        return $re;
                    } elseif($account_status == 2) {
                        $this->power = 2;
                    }
                }
            }
        }

    }
    protected function _power_msg($code) {
        $msg = array(
            '7000001' => '您的帐号已被禁用',
            '7000002' => '您的帐号审核未通过',
            '7000003' => '您的账户将在24小时内审核',
            '7000004' => '您的帐号身份变更正在审核',
            '7000005' => '您的帐号身份变更审核未通过',
        );
        $re = array(
            'returnCode' => $code,
            'returnInfo' => $msg[$code],
            'timeline' => time(),
            'content' => '',
        );
        //return $re;
        $re = array_merge($this->vdata, $re);
        header("Content-Type: application/json; charset=utf-8");
        echo json_encode($this->_send_json_befor($re));
        exit;
    }
    protected function _send_json_befor($data)
    {
        $data['content'] = isset($data['content']) ? $data['content'] : '';
        $CI  = &get_instance();
        $vdata['returnCode'] = $data['returnCode'];
        $vdata['msg'] = trim($data['returnInfo']);
        $vdata['secure'] = '0';
        if ($vdata['secure'] != '0') {
            //load_AES
            $CI->load->library('AES');
            $vdata['data'] = $data['content'] ? AES::encrypt(json_encode($data['content']), KEY) :  array();
        } else {
            $vdata['data'] = $data['content'] ? $data['content'] : array();
        }

        return $vdata;
    }
    protected function _uri_allow($str) {
        $arr = array(
            'login/three',
            'login/success',
            'account/info',
            'account/other',
            'account/get_token',
            'account/count_friend',
            'account/new_friend_fans',
            'account/rsuccess',
            'account/identity',
            'account/reportCtype',
            'account/activity',
            'circle/circle_list',
            'circle/hot_list',
            'circle/my_contents',
            'circle/circle_comments',
            'circle/company_state',
            'circle/circle_detail',
            'comments/new_comment_lists',
            'comments/do_read',
            'longarticle/longarticle_detail',
            'longarticle/circle_comments',
            'topic/topic_detail',
            'topic/circle_comments',
            'topic/news_detail',
            'collects/clist',
            'needs/my_needs',
            'needs/market_needs',
            'needs/need_detail',
            'needs/claim_list',
            'news/nlist',
            'news/browse',
            'other/userType',
            'other/interested',
            'other/company',
            'other/board',
            'concern/clist',
            'concern/cfens',
            'concern/friends',
            'concern/friend',
            'balance/blist',
            'praise/clist',
            'welcome',
            'chat/info',
            'history/hlist',
            'history/hcreate',
            'company/index',
            'common/page_detail',
            'account/account_bind'

        );
        return in_array($str,$arr) ? true : false;
    }
}